(function () {
    var Pagination = require('common:widget/ui/pagination/pagination.js').Pagination,
        Dialog = require('common:widget/ui/dialog/dialog.js').Dialog,
        util = require('common:widget/ui/libs/util.js').util,
        $page = $('.page-coupon'),
        dialog = new Dialog(),
        $recordBlock = $page.find('.record-block'),
        $recordContainer = $recordBlock.find('.record-table tbody');
    P.createPage({
        _data: {
            status: 0,
            type: 0
        },

        _create: function () {
            var me = this,
                pagination = new Pagination({
                    container: $page.find('.record-pagination'),
                    perCount: 10,
                    cur: 1,
                    onRefresh: function (curPage, callback) {
                        $.getJSON('/user/getCouponListApi', {
                            status: me.data('status'),
                            type: me.data('type'),
                            pn: curPage
                        }, function (ret, status) {
                            if (ret.error_no == 0 && ret.data) {
                                callback(ret.data.page_count * 10);
                                me._createTableList(ret.data.list);
                            }
                        });
                    }
                });
            this.data({
                'pagination': pagination,
                '$typeTab': $page.find('.type-tab a')
            });
        },

        _init: function () {
            var me = this;
            $('.ttype-tab li a').click(function () {
                $('.ttype-tab li a').removeClass('active');
                $(this).addClass('active');
                me.data('type', $(this).attr('data-type'));
                me.refresh();
            });
            $('.status-tab li a').click(function () {
                $('.status-tab li a').removeClass('active');
                $(this).addClass('active');
                me.data('status', $(this).attr('data-type'));
                me.refresh();
            });
        },

        //更新数据
        refresh: function () {
            var pagination = this.data("pagination");
            pagination.initPage(1);
        },

        //生成表格内容
        _createTableList: function (list) {
            $recordContainer.html((function () {
                var html = "";
                $.each(list, function (k, v) {
                    html += '' +
                    '<tr>' +
                    '<td>' + v.id + '</td>' +
                    '<td>' + (v.ext ? (v.type == 102 ? mathUtil.formatAmount(v.ext) * 100 + '%' : '￥' + mathUtil.formatAmount(v.ext)) : '') + P.couponMap[v.type] + '</td>' +
                    '<td>截止至' + util.toTime(v.expire_time * 1000, 'yyyy-MM-dd') + '</td>' +
                    '<td style="text-align: left">' + (v.type == 103 ? '使用后在不突破最高购买额度的基础上，可增加' + v.ext + '份活期购买额度。' : P.couponRuleMap[v.type]) + '</td>' +
                    '<td>' + (v.status == 0 ? '<div class="use_it btn btn-blank1" data-type="' + v.type + '" data-id="' + v.id + '">使用</div>' : '') + '</td>' +
                    '</tr>' +
                    ''
                });
                return html;
            })());
            $('.use_it').bind('click', function () {
                var id = $(this).attr('data-id');
                var type = $(this).attr('data-type');
                $.getJSON('/user/usecouponapi', {'id': id, 'type': type}, function (data) {
                    if (data.error_no == 0) {
                        if (type == '103') {
                            var tmpDialog = new Dialog({
                                title : '您的活期份额已提升',
                                confirmBtnText:'去购买活期',
                                cancelBtnText:'知道了',
                                confirmBtn:true,
                                onCancel: function() {
                                    location.href = '/user/coupon';
                                },
                                onConfirm:function(){
                                    location.href= '/user/buydemand';
                                }});
                            tmpDialog.show('<div>使用前活期可购份额：' + util.formatAmount(data.data.before, 2) + '。</div>' +
                            '<div style="margin-top:15px">使用后活期可购份额：<span style="color:#f89406">' + util.formatAmount(data.data.after, 2) + '</span>。</div>');
                        } else if (type == '102') {
                            var tmpDialog = new Dialog({
                                title : '本日年化收益率已提升',
                                confirmBtnText:'去购买活期',
                                cancelBtnText:'知道了',
                                confirmBtn:true,
                                onCancel: function() {
                                    location.href = '/user/coupon';
                                },
                                onConfirm:function(){
                                    location.href= '/user/buydemand';
                                }});
                            tmpDialog.show('<div>使用前活期年化收益率：' + util.formatAmount(data.data.before, 4) * 100 + '%。</div>' +
                            '<div style="margin-top:15px">使用后活期年化收益率：<span style="color:#f89406">' + util.formatAmount(data.data.after, 4) * 100 + '%</span>。</div>');
                        } else {
                            dialog.show('券使用成功。');
                        }
                    } else {
                        dialog.show(data.error_message);
                    }
                });
            });
        }
    });
})();